#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.common.validator;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Description: 校验手机号码
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Documented
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = MobilePhone.Guarder.class)
public @interface MobilePhone {

    String message() default "Call the police";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    String[] mobile() default {};

    class Guarder implements ConstraintValidator<MobilePhone, Object> {


        @Override
        public void initialize(MobilePhone constraintAnnotation) {
            // do nothing
        }

        @Override
        public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {

            if (o == null) {
                return Boolean.TRUE;
            }
            String mobile = String.valueOf(o);
            // update at 2021-03-20 添加149号段
            return mobile.matches("^1(3${symbol_escape}${symbol_escape}d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8${symbol_escape}${symbol_escape}d|9[0-35-9])${symbol_escape}${symbol_escape}d{8}${symbol_dollar}");
        }
    }

}
